package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.team.TeamHonor;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.honor.TeamHonorMapper;
import com.ruoyi.system.service.honor.ITeamHonorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * 团队荣誉Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class TeamHonorServiceImpl implements ITeamHonorService
{
    @Autowired
    private TeamHonorMapper teamHonorMapper;
    @Autowired
    private SysDeptMapper syeDeptMapper   ;

    /**
     * 查询团队荣誉
     * 
     * @param id 团队荣誉主键
     * @return 团队荣誉
     */
    @Override
    public TeamHonor selectTeamHonorById(Long id)
    {
        TeamHonor teamHonor = teamHonorMapper.selectTeamHonorById(id);
        if(teamHonor.getDeptId()!=null){
            teamHonor.setDeptName(syeDeptMapper.selectDeptById(teamHonor.getDeptId()).getDeptName());
        }
        return teamHonor;
    }

    /**
     * 查询团队荣誉列表
     * 
     * @param teamHonor 团队荣誉
     * @return 团队荣誉
     */
    @Override
    public List<TeamHonor> selectTeamHonorList(TeamHonor teamHonor)
    {
        if(teamHonor.getCurrentRole()!=null&&teamHonor.getCurrentRole().equals("department")){
            teamHonor.setDeptId(SecurityUtils.getDeptId());
        }

        return teamHonorMapper.selectTeamHonorList(teamHonor);
    }

    /**
     * 新增团队荣誉
     * 
     * @param teamHonor 团队荣誉
     * @return 结果
     */
    @Override
    @CacheEvict(
            value = "departmentStatistics_getIndexInfo",
            key = "T(com.ruoyi.common.utils.SecurityUtils).getDeptId() + '-' + T(java.time.format.DateTimeFormatter).ofPattern('yyyy').format(T(java.time.Instant).ofEpochMilli(#teamHonor.issueTime.time).atZone(T(java.time.ZoneId).systemDefault()).toLocalDate())"
    )
    public int insertTeamHonor(TeamHonor teamHonor)
    {
        Long deptId = SecurityUtils.getDeptId();
        teamHonor.setDeptId(deptId);
        return teamHonorMapper.insertTeamHonor(teamHonor);
    }

    /**
     * 修改团队荣誉
     * 
     * @param teamHonor 团队荣誉
     * @return 结果
     */
    @Override
    public int updateTeamHonor(TeamHonor teamHonor)
    {
        return teamHonorMapper.updateTeamHonor(teamHonor);
    }

    /**
     * 批量删除团队荣誉
     * 
     * @param ids 需要删除的团队荣誉主键
     * @return 结果
     */
    @Override
    public int deleteTeamHonorByIds(Long[] ids)
    {
        return teamHonorMapper.deleteTeamHonorByIds(ids);
    }

    /**
     * 删除团队荣誉信息
     * 
     * @param id 团队荣誉主键
     * @return 结果
     */
    @Override
    public int deleteTeamHonorById(Long id)
    {
        return teamHonorMapper.deleteTeamHonorById(id);
    }
}
